//
//  DcTextView.m
//  MLZJ
//
//  Created by Desire on 2019/9/4.
//  Copyright © 2019 mwyPro. All rights reserved.
//

#import "DcTextView.h"
@interface DcTextView ()
@end
@implementation DcTextView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.placeLab  = [UILabel new];
        self.placeLab.textColor = [UIColor lightGrayColor];
        [self addSubview:self.placeLab];
        self.placeLab.textAlignment = NSTextAlignmentRight;
        [self.placeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.offset(5);
            make.width.equalTo(self.mas_width).offset(-10);
        }];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beginChange) name:UITextViewTextDidBeginEditingNotification object:self];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChange) name:UITextViewTextDidChangeNotification object:self];
        
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    self.placeLab  = [UILabel new];
    self.placeLab.textColor = [UIColor lightGrayColor];
    self.placeLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.placeLab];
    self.placeLab.textAlignment = NSTextAlignmentLeft;
    [self.placeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(5);
        make.width.equalTo(self.mas_width).offset(-10);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beginChange) name:UITextViewTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChange) name:UITextViewTextDidChangeNotification object:self];
}
-(void)setFont:(UIFont *)font {
    [super setFont:font];
    self.placeLab.font = font;
}
-(void)setTextAlignment:(NSTextAlignment)textAlignment{
    [super setTextAlignment:textAlignment];
    self.placeLab.textAlignment = textAlignment;
}


-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.placeLab.text = placeholder;
}

-(void)beginChange{
    if([self.text isEqualToString:@""]){
        self.placeLab.hidden = NO;
    }else{
        self.placeLab.hidden = YES;
    }
}
-(void)didChange{
    if([self.text isEqualToString:@""]){
        self.placeLab.hidden = NO;
    }else{
        self.placeLab.hidden = YES;
    }
}
-(void)setText:(NSString *)text{
    [super setText:text];
    [self beginChange];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
